var studentQueryUrl = basePath + "/sys/student/manage/json/studentList";

var dataGrid;
$(function() {
	initGradeList();
	loadData();
});

function loadData() {
	dataGrid = $('#studentDg').datagrid({
		url : studentQueryUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		nowrap : true,
		striped : true,
		rownumbers : true,
		queryParams: {
			grade: $("#grade").val()
		},
		columns : [ [  {
			field : 'id',
			width : 20,
			align : "center",
			checkbox : true
		}, {
			field : 'name',
			title : '姓名',
			resizable : true,
			align : "center",
			width : 20
		}, {
			field : 'sex',
			title : '性别',
			width : 20,
			align : "center"
		}, {
			field : 'enterYear',
			title : '年级',
			width : 20,
			align : "center",
			formatter: function(value) {
				return value + "级";
			}
		}, {
			field : 'clazz',
			title : '班级',
			width : 20,
			align : "center"
		}, {
			field : 'organize',
			title : '组织路径',
			width : 20,
			align : "center"
		}, {
			field : 'no',
			title : '学工号',
			width : 20,
			align : "center"
		}, {
			field : 'phone',
			title : '手机号码',
			width : 20,
			align : "center"
		}, {
			field : 'birthday',
			title : '出生日期',
			width : 20,
			align : "center"
		}, {
			field : 'nation',
			title : '民族',
			width : 20,
			align : "center"
		}, {
			field : 'enterYear',
			title : '入学年份',
			width : 20,
			align : "center"
		}, {
			field : 'idType',
			title : '证件类型',
			width : 20,
			align : "center"
		}, {
			field : 'idNo',
			title : '证件号码',
			width : 20,
			align : "center"
		}, {
			field : 'personType',
			title : '人员类型',
			width : 20,
			align : "center"
		}, {
			field : 'faceImgUrl',
			title : '人脸照片',
			width : 20,
			align : "center",
			formatter: function(value) {
				if(value) {
					return "<a href="+ imgUrl + value +">查看</a>";
				}
				return "无";
			}
		}] ]
		
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load');
}

function searchForm(formId) {
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function initGradeList() {
	$.ajax({
 		url : basePath + "/getFullGrade",
 		type : "post",
 		dataType : "json",
 		async : false,
 		success : function(data) {
 			var html = '<option value=""></option>';
 			for(var i in data){
 		        if (data.hasOwnProperty(i)) {
 		        	html += "<option value='"+data[i]+"' "+(i==(data.length-1)?"selected='selected'":"")+">"+ data[i] +"级</option>";
 		        };
 		    }
 			$("#grade").html(html);
 			$("#grade").trigger("onchange");
 		}, error:function(){
 			alert("查询年级异常");
  		}
    });
}

function searchClazz(grade) {
	if(!grade) return;
	$.ajax({
 		url : basePath + "/getFullClazz?grade=" + grade,
 		type : "GET",
 		dataType : "json",
 		async : false,
 		success : function(data) {
 			var html = '<option value=""></option>';
 			for(var i in data){
 		        if (data.hasOwnProperty(i)) {
 		        	html += "<option value='"+data[i]+"'>"+ data[i] +"班</option>";
 		        };
 		    }
 			$("#clazz").html(html);
 		}, error:function(){
 			alert("查询班级异常");
  		}
    });
}

function exportToExcel(searchForm,exportUrl) {
	$("#" + searchForm).attr('action',exportUrl);
	$("#" + searchForm).submit();  
}